/*
 * Saphre - Suffix Arrays for Phrase Extraction
 * Copyright (C) 2013 
 * Dale Gerdemann - Tübingen, Germany 
 * Niko Schenk - Frankfurt am Main, Germany
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package util.sorting;

import java.util.Arrays;

public class IterativeSort {

    public int[] sortItMan(int[] unsorted) {
        System.out.println("Started preparing sorting...");

        int length = unsorted.length;

        String[] toSort = new String[unsorted.length];
        for (int i = 0; i < unsorted.length; i++) {
            System.out.println(i + "/" + unsorted.length);
            StringBuilder sb = new StringBuilder();
            for (int j = i; j < unsorted.length; j++) {
                sb.append(unsorted[j]);
            }
            toSort[i] = sb.toString();
        }

        System.out.println("Started sorting...");
        Arrays.sort(toSort);
        System.out.println("Finished sorting.");

        int[] rval = new int[length];
        rval[0] = 0;
        for (int i = 0; i < toSort.length - 1; i++) {
            int suffixLength = toSort[i].length();
            rval[i + 1] = length - suffixLength;
        }

        System.out.println("...finished postprocessing.");
        return rval;
    }

    public static void main(String[] args) {
        String corpus = "$mining_engineering$$";

        // unsorted input replaced with numbers for characters and sentinels.
        //int[] unsorted = {9,3,2,4,2,4,1,6,0,4,1,2,4,0,0,5,2,4,1,7,8};
        int[] unsorted = {7, 1, 0, 2, 0, 2, 0, 4, 0, 2, 0, 2, 0, 3, 5, 6};


        //0 8 2 10 4 12 6 1 9 3 11 5 13 7 14 15 

        int length = unsorted.length;
        String[] toSort = new String[unsorted.length];
        // TODO: Sort this string.
        for (int i = 0; i < unsorted.length; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = i; j < unsorted.length; j++) {
                sb.append(unsorted[j]);
            }
            //System.out.println();
            toSort[i] = sb.toString();
        }

        for (int i = 0; i < toSort.length; i++) {
            System.out.println(toSort[i]);
        }

        Arrays.sort(toSort);


        int[] rval = new int[length];
        rval[0] = 0;
        for (int i = 0; i < toSort.length - 1; i++) {
            int suffixLength = toSort[i].length();
            System.out.println(length - suffixLength + " -- " + toSort[i]);
            rval[i + 1] = length - suffixLength;
        }


        System.out.println("Sorted: ");
        for (int i : rval) {
            System.out.print(i + " ");
        }

        // sorted input. // ingore first one.
        int[] sorted = {0, 13, 8, 14, 10, 6, 18, 11, 4, 16, 2, 1, 12, 9, 5, 17, 3, 15, 7, 19, 20};

        for (int i = 0; i < sorted.length; i++) {
        //System.out.println(corpus.substring(sorted[i]));
        }
    }
}
